import Ember from 'ember';
import PatchFieldMixin from 'emberworkspace/mixins/patch-field';
import FormState from 'emberworkspace/utils/form-state';
import ItemViewMinxin from 'emberworkspace/mixins/item-view-mixin';

export default Ember.Controller.extend(PatchFieldMixin, ItemViewMinxin, {
  pageTitle: "用户管理",
  pageSubTitle: "编辑用户",
  modelName: 'person',
  menus: Ember.computed(function() {
    return this.commonMenus;
  }),

  formState: FormState.create({
    btnName: "保存",
    successMsg: "成功",
    failMsg: "失败"
  }),
  actions: {
    menuClicked(mname) {
        this.defaultMenuHandler(mname);
      },
      roleChanged(b) {
        // console.log(b);
        if (b) {
          this.get('_patch').addObject("roles");
        } else {
          this.get('_patch').removeObject("roles");
        }
      },
      save() {
        var model = this.get('model'),
          self = this,
          formState = this.get('formState');

        if (!formState.allowSubmit()) {
          return;
        }
        this.sumupPatch();
        model.save().then(function() {
          self.set('_patch', Ember.A());
          formState.success();
        }, function() {
          formState.failure();
        });
      }
  }
});
